/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.ac.cintcm.sh.domain.Article;
import cn.ac.cintcm.sh.domain.Chapter;
import cn.ac.cintcm.sh.domain.Chapters;

public class ArticleDao {
    private HsqldbDao dao = (new HsqldbDao()).getDao();

    
    public ArticleDao() {
    
    }
    
    public List<Article> getArticles(int chapterId) {
        String sql="select article.id, article.text from " +
                "article, chapter where article.chapter_id="+chapterId + " and chapter.id=article.chapter_id";
        Article a=null;
        Chapter c = new Chapter();
        c.setId(chapterId);
        this.dao.openConnection();
        ResultSet rs=this.dao.executeQuery(sql);
        try{
            while(rs.next()){
                int articleId = rs.getInt(1);
                String content=rs.getString(2);
                a = new Article();
                a.setId(articleId);
                a.setText(content);
                c.addArticle(a);
             }
        }catch(Exception e){
            e.printStackTrace();
        }
        this.dao.closeConnection();
        return c.getArticles();    
    }

    public Chapters getArticles(String name) {
        Chapters chapter = new Chapters();
        String sql="select article.id, article.text, article.chapter_id, chapter.name " +
                "from article, chapter where article.text like '%"+ name + "%' and chapter.id=article.chapter_id";
        this.dao.openConnection();
        ResultSet rs=this.dao.executeQuery(sql);
        HashMap<Integer,Chapter> chapters = new  HashMap<Integer,Chapter>();
        try{
            while(rs.next()){
                int articleId = rs.getInt(1);
                String content=rs.getString(2);
                int chapterId = rs.getInt(3);
                String cn = rs.getString(4);
                Article a = new Article();
                a.setId(articleId);
                a.setText(content);
                Chapter c = chapters.get(chapterId);
                if (c == null) {
                    c = new Chapter();
                    c.setId(chapterId);
                    c.setName(cn);
                    chapters.put(chapterId, c);
                }
                c.addArticle(a);
            }
            chapter.setChapters(chapters);
        }catch(Exception e){
            e.printStackTrace();
        }
        this.dao.closeConnection();
        return chapter;
    }

    public List<Article> getPresArticles(int prescriptionId) {
        ArrayList<Article> articles = new ArrayList<Article>();
        String sql = "select ARTICLE.id, ARTICLE.text from ARTICLE_PRESCRIPTION, ARTICLE where PRESCRIPTION_ID="
            + prescriptionId + " and ARTICLE.id=ARTICLE_PRESCRIPTION.ARTICLE_ID";;
        this.dao.openConnection();
        ResultSet rs = this.dao.executeQuery(sql);
        try{
            while(rs.next()){
                int articleId = rs.getInt(1);
                String content = rs.getString(2);
                Article a = new Article();
                a.setId(articleId);
                a.setText(content);
                articles.add(a);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        this.dao.closeConnection();
        return articles;
    }

    public List<Article> getHerbArticles(int herbId) {
        ArrayList<Article> articles = new ArrayList<Article>();
        String sql = "select ARTICLE.id, ARTICLE.text from ARTICLE_PRESCRIPTION, ARTICLE, HERB_IN_PRESCRIPTION where HERB_IN_PRESCRIPTION.ID="
            + herbId + " and ARTICLE.id=ARTICLE_PRESCRIPTION.ARTICLE_ID and ARTICLE_PRESCRIPTION.PRESCRIPTION_ID=HERB_IN_PRESCRIPTION.PRESCRIPTION_ID";;
        this.dao.openConnection();
        ResultSet rs = this.dao.executeQuery(sql);
        try{
            while(rs.next()){
                int articleId = rs.getInt(1);
                String content = rs.getString(2);
                Article a = new Article();
                a.setId(articleId);
                a.setText(content);
                articles.add(a);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        this.dao.closeConnection();
        return articles;
    }

    public List<Article> getArticles(int[] ids) {
        ArrayList<Article> articles = null;
       if(ids != null && ids.length > 0) {
            articles=new ArrayList<Article>();
            this.dao.openConnection();
            StringBuffer sql = new StringBuffer("select * from Article where id in(");
            for (int id : ids) {
                sql.append(id);
                sql.append(",");
            }
            sql.deleteCharAt(sql.length() - 1);
            sql.append(")");
            ResultSet rs = dao.executeQuery(sql.toString());
            try{
                while(rs.next()){
                    int id = rs.getInt("id");
                    String content = rs.getString("text");
                    Article a = new Article();
                    a.setId(id);
                    a.setText(content);
                    articles.add(a);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            this.dao.closeConnection();
        }
        return articles;
    }

}